# Sets CPU_ARCH and PLATFORM_ARCH variables in the calling scope.
function(set_cpu_platform_arch)
    if(${CMAKE_CXX_COMPILER} MATCHES "x86/cl.exe$")
        set(CPU_ARCH "x86" PARENT_SCOPE)
        set(PLATFORM_ARCH "win" PARENT_SCOPE)
    elseif(${CMAKE_CXX_COMPILER} MATCHES "x64/cl.exe$")
        set(CPU_ARCH "x64" PARENT_SCOPE)
        set(PLATFORM_ARCH "win" PARENT_SCOPE)
    elseif(${CMAKE_CXX_COMPILER} MATCHES "arm/cl.exe$")
        set(CPU_ARCH "ARM" PARENT_SCOPE)
        set(PLATFORM_ARCH "win" PARENT_SCOPE)
    elseif(${CMAKE_CXX_COMPILER} MATCHES "arm64/cl.exe$")
        set(CPU_ARCH "ARM64" PARENT_SCOPE)
        set(PLATFORM_ARCH "win" PARENT_SCOPE)
    elseif(${CMAKE_CXX_COMPILER} MATCHES "windows-x86_64/bin/clang\\+\\+.exe$")
        set(CPU_ARCH "x64" PARENT_SCOPE)
        set(PLATFORM_ARCH "win" PARENT_SCOPE)
    elseif(${CMAKE_CXX_COMPILER} MATCHES "linux-x86_64/bin/clang\\+\\+$")
        set(CPU_ARCH "x64" PARENT_SCOPE)
        set(PLATFORM_ARCH "linux" PARENT_SCOPE)
    elseif(${CMAKE_CXX_COMPILER} MATCHES "darwin-x86_64/bin/clang\\+\\+$")
        set(CPU_ARCH "x64" PARENT_SCOPE)
        set(PLATFORM_ARCH "darwin" PARENT_SCOPE)
    else()
        message(FATAL_ERROR "Unrecognized compiler: ${CMAKE_CXX_COMPILER}")
    endif()
endfunction()

# Uses the nuget.config and packages.config files in the current directory to download packages from NuGet.
# NUGET_PATH will be set to the directory where the packages are installed.
function(download_nuget)
    set(NUGET_PATH "${CMAKE_BINARY_DIR}/NuGet")
    set(NUGET_PATH "${NUGET_PATH}" PARENT_SCOPE)
    set(NUGET_EXE "${NUGET_PATH}/nuget.exe")
    if(NOT EXISTS ${NUGET_EXE})
        file(DOWNLOAD "https://dist.nuget.org/win-x86-commandline/latest/nuget.exe" ${NUGET_EXE})
    endif()
    file(COPY "nuget.config" DESTINATION ${NUGET_PATH})
    file(COPY "packages.config" DESTINATION ${NUGET_PATH})

    execute_process(COMMAND ${NUGET_EXE} restore WORKING_DIRECTORY ${NUGET_PATH})
    execute_process(COMMAND ${NUGET_EXE} install WORKING_DIRECTORY ${NUGET_PATH})
endfunction()
